package module.map.scenes
{
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.values.CollectionValue;

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	import drongo.ddls2d.ai.DDLSPathFinder;
	import drongo.ddls2d.data.DDLSMesh;
	import drongo.ddls2d.factories.DDLSRectMeshFactory;
	import drongo.ddls2d.view.DDLSSimpleView;

	import module.map.model.MapModel;
	import module.map.model.NodeTypes;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	import module.map.utils.PathUtils;
	import drongo.ddls2d.ai.DDLSEntityAI;
	import flash.events.Event;

	public class TestLayer extends Sprite
	{
		private static var egde:Vector.<Number> = new Vector.<Number>();
		private static var nav:Vector.<Number> = new Vector.<Number>();
		private static var findPath:Vector.<Number> = new Vector.<Number>();
		// 导航网格测试
		private var navView:DDLSSimpleView;
		private var pathfinder:DDLSPathFinder;

		private var project:ProjectModel;
		private var startPoint:Point = new Point();
		private var endPoint:Point = new Point();
		private var mapRect:Rectangle;
		private var playerShape:Shape = new Shape;

		private var isStart:Boolean = false;
		private var mesh:DDLSMesh;
		public function TestLayer(project:ProjectModel)
		{
			super();
			this.project = project;

			this.playerShape = new Shape();
			this.addChild(this.playerShape);

			this.navView = new DDLSSimpleView();
			this.AddEvents();
		}

		public function Destory():void
		{
			this.project = null;
			this.RemoveEvents();
		}

		private function AddEvents():void
		{

		}

		private function RemoveEvents():void
		{

		}
		
		public function Refresh():void
		{
			var currentMap:MapModel = this.project.currentMap;
			if (currentMap == null)
			{
				this.removeChild(this.navView.surface);
				this.removeChild(this.playerShape);
				return;
			}
			// 测试中
			if (currentMap.Get(Propertys.TEST).GetValue() == 1)
			{
				this.addChild(this.navView.surface);
				this.addChild(this.playerShape);

				var w:Number = currentMap.Get(Propertys.WIDTH).GetValue();
				var h:Number = currentMap.Get(Propertys.HEIGTH).GetValue();

				var pw:Number = currentMap.Get(Propertys.NAV_WIDTH).GetValue();
				var ph:Number = currentMap.Get(Propertys.NAV_HEIGTH).GetValue();

				mapRect = new Rectangle();
				mapRect.x = 0;
				mapRect.y = 0;
				mapRect.width = w + pw * 2;
				mapRect.height = h + ph * 2;

				this.mesh = DDLSRectMeshFactory.buildRectangle(w + pw * 2, h + ph * 2);

				// 构建导航网格
				BuildNav(currentMap, mesh);
				this.navView.drawMesh(mesh);

				pathfinder ||= new DDLSPathFinder;
				pathfinder.mesh = mesh;

				// 监听事件
				stage.addEventListener(MouseEvent.CLICK, onClick);
			}
			else
			{
				if (this.navView.surface.parent)
				{
					this.removeChild(this.playerShape);
					this.removeChild(this.navView.surface);
				}
				// 监听事件
				stage.removeEventListener(MouseEvent.CLICK, onClick);
			}
		}

		private function onClick(e:MouseEvent):void
		{
			this.endPoint.x = e.stageX;
			this.endPoint.y = e.stageY;
			this.endPoint = this.globalToLocal(endPoint);
			// trace(e.stageX, e.stageY);
			// trace(this.endPoint.x, this.endPoint.y);

			// 如果没有点在寻路范围内
			if (!mapRect.contains(this.endPoint.x, this.endPoint.y))
			{
				return;
			}
			this.isStart = !this.isStart;
			if (isStart)
			{
				this.startPoint.x = this.endPoint.x;
				this.startPoint.y = this.endPoint.y;
				// 绘制
				playerShape.graphics.clear();
				playerShape.graphics.beginFill(0xFF0000);
				playerShape.graphics.drawRect(startPoint.x - 25 / 2, startPoint.y - 25 / 2, 25, 25);
				playerShape.graphics.endFill();
			}
			else
			{
				playerShape.graphics.beginFill(0x0000FF);
				playerShape.graphics.drawRect(this.endPoint.x - 25 / 2, this.endPoint.y - 25 / 2, 25, 25);
				playerShape.graphics.endFill();

				pathfinder.findPath(startPoint.x, startPoint.y, this.endPoint.x, this.endPoint.y, findPath,10);

				playerShape.graphics.lineStyle(2, 0x00ffff);
				for (var i:int = 0; i < findPath.length; i += 2)
				{
					if (i == 0)
					{
						playerShape.graphics.moveTo(findPath[i], findPath[i + 1]);
					}
					else
					{
						playerShape.graphics.lineTo(findPath[i], findPath[i + 1]);
					}
				}
			}
		}

		private function BuildNav(currentMap:MapModel, mesh:DDLSMesh):void
		{
			var nodes:ArrayProperty = currentMap.nodes;
			var nodeType:uint;
			var node:CollectionValue;
			for (var i:int = 0; i < nodes.length; i++)
			{
				node = nodes.GetAt(i) as CollectionValue;
				nodeType = node.Get(Propertys.TYPE).GetValue();
				switch (nodeType)
				{
					case NodeTypes.AREA:
					{
						this.BuildAreaNav(currentMap, node, mesh);
						break;
					}
					default:
					{
						break;
					}
				}
			}
		}

		private function BuildAreaNav(currentMap:MapModel, area:CollectionValue, mesh:DDLSMesh):void
		{
			egde.length = nav.length = 0;
			var points:ArrayProperty = area.Get(Propertys.POINTS) as ArrayProperty;
			var point:CollectionValue;
			var x:Number = currentMap.navWidth + area.Get(Propertys.X).GetValue();
			var y:Number = currentMap.navHeight + area.Get(Propertys.Y).GetValue();
			for (var i:int = 0; i < points.length; i++)
			{
				point = points.GetAt(i) as CollectionValue;
				egde.push(
						x + point.Get(Propertys.X).GetValue(),
						y + point.Get(Propertys.Y).GetValue()
					);
			}
			point=points.GetAt(0) as CollectionValue;
			egde.push(
				x+point.Get(Propertys.X).GetValue(),
				y+point.Get(Propertys.Y).GetValue()
			);
			PathUtils.Edge2Nav(egde, nav);
			trace(nav);
			mesh.insertConstraintShape(nav);
		}
	}
}